package com.zhenyi.core.tencent.map;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.wxpay.utils.MD5Util;

public class MapUtils {

	private String key;
	private String signKey;
	private final String HOST = "https://apis.map.qq.com";

	public MapUtils(String key, String signKey) {
		this.key = key;
		this.signKey = signKey;
	}

	/**
	 * 生成配送中地图
	 * 
	 * @param userLocation
	 *            22.250901,113.584471
	 * @param staffLocation
	 *            22.250901,113.584471
	 */
	public String getDeliverMap(String userLocation, String staffLocation) {
		IdentityHashMap<String, String> map = new IdentityHashMap<>();
		String size = "600*300";
		size = "600*600";
		String maptype = "roadmap";
		String icon = "http://res.zhen-yee.com/res/www/img/favicon.png";
		// icon = URLUtil.encode(icon);
		String markers = "size:large|color:0xFFCCFF|icon:" + icon + "|" + staffLocation + "|" + userLocation;
		// markers = markers + "|icon:" + icon;

		String labels = "border:1|size:12|color:0x000000|bgcolor:0xFFFFFF|anchor:3|offset:0_-15|植绘配送|" + staffLocation;
		String path = "/ws/staticmap/v2";
		// map.put("center", center);
		// map.put("zoom", zoom);
		map.put("size", size);
		map.put("maptype", maptype);
		map.put("markers", markers);
		map.put("labels", labels);

		return getMapUrl(map, path);
	}

	/**
	 * 生成地图连接
	 * 
	 * @param params
	 *            地图参数
	 * @param path
	 *            /ws/staticmap/v2
	 */
	public String getMapUrl(Map<String, String> params, String path) {
		params.put("key", key);
		String baseUrl = "";
		// 先将参数以其参数名的字典序升序进行排序
		TreeMap<String, String> sortedParams = new TreeMap<String, String>(params);
		// 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Entry<String, String> param : sortedParams.entrySet()) {
			String paramKey = param.getKey();
			String value = param.getValue();
			if (StrKit.isBlank(value)) {
				continue;
			}
			if (first) {
				first = false;
			} else {
				sb.append("&");
			}
			sb.append(paramKey).append("=");
			sb.append(value);
			baseUrl = baseUrl + paramKey + "=" + value + "&";
		}
		sb.append(signKey);

		String signString = path + "?" + sb.toString();
		System.out.println(signString);
		String sign = MD5Util.MD5Encode(signString, "utf-8");
		System.out.println(sign);
		String url = HOST + path + "?" + baseUrl + "sig=" + sign;
		// return sb.toString();
		System.out.println(url);
		return url;
	}

	public String sign(Map<String, String> params) {
		// 先将参数以其参数名的字典序升序进行排序
		TreeMap<String, String> sortedParams = new TreeMap<String, String>(params);
		// 遍历排序后的字典，将所有参数按"key=value"格式拼接在一起
		StringBuilder sb = new StringBuilder();
		boolean first = true;
		for (Entry<String, String> param : sortedParams.entrySet()) {
			String value = param.getValue();
			if (StrKit.isBlank(value)) {
				continue;
			}
			if (first) {
				first = false;
			} else {
				sb.append("&");
			}
			sb.append(param.getKey()).append("=");
			sb.append(value);
		}
		return MD5Util.MD5Encode(sb.toString(), "utf-8");
		// return sb.toString();
	}

	public static void main(String[] args) {
		IdentityHashMap<String, String> map = new IdentityHashMap<>();
		String center = "39.8802147,116.415794";

		String center2 = "22.253908157348633,113.57881927490234";
		center2 = "22.250901,113.584471";// ditu
		center2 = "22.253957,113.578457";//
		center2 = "22.250594075520834,113.58383978949652";// gcj02
		center = "22.2533016204834,113.57396697998047";
		String zoom = "16";
		String size = "600*600";
		size = "600*600";
		String maptype = "roadmap";
		String icon = "http://res.zhen-yee.com/res/www/img/favicon.png";
		// icon = URLUtil.encode(icon);
		String markers = "size:large|color:0xFFCCFF|label:植|" + center;
		markers = "size:large|color:0xFFCCFF|icon:" + icon + "|" + center + "|" + center2;
		// markers = markers + "|icon:" + icon;

		String labels = "border:1|size:12|color:0x000000|bgcolor:0xFFFFFF|anchor:3|offset:0_-15|植绘配送|" + center;
		String path = "/ws/staticmap/v2";
		// map.put("center", center);
		// map.put("zoom", zoom);
		map.put("size", size);
		map.put("maptype", maptype);
		map.put("markers", markers);
		map.put("labels", labels);
		System.out.println(JsonKit.toJson(map));
		String appKey = "U4HBZ-DYHRS-P3COG-6AAHE-OEFCV-ECFMY";
		String signKey = "jtuzfmTEDoUNzayknQwANPFl5Hnx8Au";
		// map.put("key", appKey);

		// map.put("key", "5Q5BZ-5EVWJ-SN5F3-K6QBZ-B3FAO-RVBWM");
		// map.put("location", "28.7033487,115.8660847");
		// appKey = "5Q5BZ-5EVWJ-SN5F3-K6QBZ-B3FAO-RVBWM";
		// signKey = "SWvT26ypwq5Nwb5RvS8cLi6NSoH8HlJX";
		// path = "/ws/geocoder/v1";
		// new MapUtils(appKey, signKey).getMapUrl(map, path);

		new MapUtils(appKey, signKey).getDeliverMap("22.250594075520834,113.58383978949652",
				"22.2533016204834,113.57396697998047");
	}
}
